from sqlalchemy import create_engine
from SpiffGuard import *

class Passenger(Resource):
    pass

class PassengerGroup(ResourceGroup):
    pass

class Room(Resource):
    pass

db    = create_engine('mysql://tester:test@localhost/unit_tests')
guard = DB(db)
guard.install()
guard.register_type([Passenger, PassengerGroup, Room, Action])

passengers = PassengerGroup("Passenger Group")
crew       = PassengerGroup("Crew Members")
engineers  = PassengerGroup("Engineers")
guests     = PassengerGroup("Guests")
guard.add_resource(None, passengers)
guard.add_resource(passengers, crew)
guard.add_resource(crew, engineers)
guard.add_resource(passengers, guests)

han  = Passenger("Han Solo")
r2d2 = Passenger("R2D2")
luke = Passenger("Luke Skywalker")
guard.add_resource(engineers, han)
guard.add_resource(engineers, r2d2)
guard.add_resource(crew, luke)

lounge = Room("Lounge")
bridge = Room("Bridge")
mr     = Room("Machine Room")
guard.add_resource(None, lounge)
guard.add_resource(None, bridge)
guard.add_resource(None, mr)

enter  = Action("Enter")
repair = Action("Repair")
guard.add_action([enter, repair])

guard.grant(crew,      enter,  bridge)
guard.grant(engineers, enter,  mr)
guard.grant(engineers, repair, mr)
guard.grant(guests,    enter,  lounge)
guard.deny(han, repair, mr)
